package gui.item;

import common.util.DateUtils;

import core.Item;
import core.ItemFacade;
import core.exception.ModelException;
import gui.common.*;

/**
 * Controller class for the edit item view.
 */
public class EditItemController extends Controller 
										implements IEditItemController {
	
	private EditItemView _theView;
	private ItemFacade _iFacade;
	
	private boolean entryDateValid;
	
	private ItemData _item;
	
	/**
	 * Constructor.
	 * 
	 * @param view Reference to edit item view
	 * @param target Item that is being edited
	 */
	public EditItemController(IView view, ItemData target) {
		super(view);
		
		entryDateValid = true;
		
		_theView = (EditItemView) this._view;
		_item = target;
		_iFacade = ItemFacade.instance();
		construct();
	}

	//
	// Controller overrides
	//
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IEditItemView getView() {
		return (IEditItemView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() {
		//add item button
		boolean enableOkButton = true;
		if(!entryDateValid){
			enableOkButton = false;
		}
		_theView.enableOK(enableOkButton);
		
		//always disable the item description and the item barcode
		_theView.enableBarcode(false);
		_theView.enableDescription(false);
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() {
		//get a ProductData
		String description;
		try {
			description = _iFacade.getItem(_item.getBarcode()).getProduct().getDescription();
			//set the Item description (from the product)
			_theView.setDescription(description);
			//set the Item barcode
			_theView.setBarcode(_item.getBarcode());
			//set the entry date
			_theView.setEntryDate(_item.getEntryDate());
		} catch (ModelException e) {
			_theView.displayErrorMessage(e.getMessage());
		}
		
	}

	//
	// IEditItemController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * edit item view is changed by the user.
	 */
	@Override
	public void valuesChanged() {
		if (_theView.getEntryDate() != null){
			String formattedDate = DateUtils.formatDate(_theView.getEntryDate());
			entryDateValid = Item.isValidDate(formattedDate);
		}
		else{
			entryDateValid = false;
		}
		enableComponents();
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the edit item view.
	 */
	@Override
	public void editItem() {
		try {
			_iFacade.editItem(_item.getItem(),DateUtils.formatDate(_theView.getEntryDate()));
		} catch (ModelException e) {
			_theView.displayErrorMessage(e.getMessage());
		}
	}

}

